﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Security.Principal;
using System.Net;
using System.Configuration;
using Microsoft.Reporting.WebForms;

namespace Crosland.Automotriz.Web.Vehiculos
{
    public partial class wFrmVentaExternaReporte : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (Page.IsPostBack == false)
            {
                this.MuestraReporte();

            }
        }


        private void MuestraReporte()
        {
            try
            {
                String strUserCliente = Session["UserCliente"].ToString();
                String strAnioMesIni = Session["AnioMesIni"].ToString();
                String strAnioMesFin = Session["AnioMesFin"].ToString();
                rvGeneral.ServerReport.ReportServerCredentials = new MyReportServerCredentials();
                rvGeneral.ProcessingMode = ProcessingMode.Remote;
                rvGeneral.ServerReport.ReportServerUrl = new Uri(ConfigurationManager.AppSettings["RsServer"]);
                rvGeneral.ServerReport.ReportPath = "/SAP_Vehiculos/rsVentaExterna";
                ReportParameter parIdUsuario = new ReportParameter("ID_USUARIO", strUserCliente);
                ReportParameter parAnioMesIni = new ReportParameter("ANIO_MES_INI", strAnioMesIni);
                ReportParameter parAnioMesFin = new ReportParameter("ANIO_MES_FIN", strAnioMesFin);
                this.rvGeneral.ServerReport.SetParameters(parIdUsuario);
                this.rvGeneral.ServerReport.SetParameters(parAnioMesIni);
                this.rvGeneral.ServerReport.SetParameters(parAnioMesFin);
                rvGeneral.ServerReport.Refresh();
            }
            catch (Exception ex)
            {
                lblMensaje.Text = "Error en la aplicacón "+ex.Message;
                popupErrores.ShowOnPageLoad = true;
            }

                

        }
        
        [Serializable]
        public sealed class MyReportServerCredentials : IReportServerCredentials
        {
            public WindowsIdentity ImpersonationUser
            {
                get
                {

                    return null;
                }
            }

            public ICredentials NetworkCredentials
            {
                get
                {


                    // User name
                    string userName = ConfigurationManager.AppSettings["RSUser"];

                    if (string.IsNullOrEmpty(userName))
                        throw new Exception(
                            "Missing user name from web.config file");

                    // Password
                    string password =
                        ConfigurationManager.AppSettings["RSPassword"];

                    if (string.IsNullOrEmpty(password))
                        throw new Exception(
                            "Missing password from web.config file");

                    // Domain
                    string domain =
                        ConfigurationManager.AppSettings["RSDomain"];

                    if (string.IsNullOrEmpty(domain))
                        throw new Exception(
                            "Missing domain from web.config file");

                    return new NetworkCredential(userName, password, domain);
                }
            }

            public bool GetFormsCredentials(out Cookie authCookie,
                        out string userName, out string password,
                        out string authority)
            {
                authCookie = null;
                userName = null;
                password = null;
                authority = null;


                return false;
            }


        }





    }

}
